package com.znsd.zl.app.ocr.controller;

import com.znsd.zl.app.ocr.service.IOcrService;
import com.znsd.zl.exception.RRException;
import com.znsd.zl.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 *     百度OCR识别控制器
 * </p>
 * @ClassName SampleController
 * @Description
 * @Author jaypdd
 * @Date 2020/8/29 10:17
 * @Version v1.0
 */
@RestController
@RequestMapping("/user/sample")
@Slf4j
public class SampleController {

	@Resource
	private IOcrService ocrService;

	/**
	 * 用户实名认证
	 * @param userId    认证的用户id
	 * @param files     实名的身份证正反证件照
	 * @return          成功返回：code：0，失败：500
	 */
	@PostMapping("/realName")
	public R idCardUpload(@RequestParam("userId")Integer userId, @RequestParam("files") MultipartFile[] files) {
		int fileNumber = 2;
		if ( files.length != fileNumber) {
			throw new RRException("身份证上传失败，请稍后重试!");
		}
		try {
			boolean authStatus = ocrService.realNameAuthentication(userId, files);
			if ( authStatus ) {
				return R.ok("实名认证成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.error("实名认证失败，身份证信息有误");
	}


}
